package com.nd.mobile.study.view;

import android.content.Context;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnPreparedListener;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.MediaController;
import android.widget.VideoView;

import com.nd.mobile.study.R;
import com.nd.mobile.study.common.App;
import com.nd.mobile.study.common.BaseActivity;
import com.nd.mobile.study.common.SimpleAsyncTask;
import com.nd.mobile.study.common.constants.BundleConstants;
import com.nd.mobile.study.common.exception.LocalErrorException;
import com.nd.mobile.study.common.util.ExceptionHandler;
import com.nd.mobile.study.common.util.L;
import com.nd.mobile.study.common.util.SharedPreferencesUtil;
import com.nd.mobile.study.domain.User;
import com.nd.mobile.study.domain.VideoInfo;
import com.nd.mobile.study.view.helper.ToastHelper;

public class VideoPlayerActivity extends BaseActivity implements OnClickListener, OnPreparedListener {

	// private String path =
	// "http://172.21.5.213:8080/ty/201105271801480006_13_480x320.mp4";
	// private String path =
	// "http://192.168.3.118:80/Struts2Demo/238-mongoid.mp4";

	private boolean					mIsMenuShown;
	private MediaController			mMediaController;

	private VideoView				mVideoView;
	private int						mBeginTime;
	private VideoInfo				mVideoInfo;
	private SharedPreferencesUtil	mSp;
	private User					mUser;

	@Override
	protected void onBaseCreate(Bundle savedInstanceState) {
		setContentView(R.layout.videoplayer);
	}

	@Override
	protected void onAfterCreate(Bundle savedInstanceStat) {
		mLoading.p();
		getIntentData();
		mVideoView.setVideoURI(Uri.parse(mVideoInfo.getResURL()));
		// mVideoView.setVideoURI(Uri.parse(path));
		mMediaController = new MediaController(this);
		mVideoView.setMediaController(mMediaController);
		mVideoView.setOnClickListener(this);
		mVideoView.setOnPreparedListener(this);
		mVideoView.requestFocus();
	}

	@Override
	protected void setFieldValues() {
		mUser = null;
		try {
			mUser = App.getInstance().getUser();
		} catch (LocalErrorException e) {
			e.printStackTrace();
			String errorMsg = ExceptionHandler.parseExceptionInfo(e);
			ToastHelper.displayToast(this, errorMsg);
		}
		mSp = SharedPreferencesUtil.getUserViedoInfoSp(this, mUser);
	}

	@Override
	protected void initViews() {
		mVideoView = (VideoView) findViewById(R.id.video_view);
	}

	private void getIntentData() {
		mBeginTime = getIntent().getIntExtra(BundleConstants.VIDEO_BEGIN_TIME, 0);
		L.v(this.getClass(), "getIntentData mBeginTime = " + mBeginTime);
		mVideoInfo = (VideoInfo) getIntent().getSerializableExtra(BundleConstants.VIDEO_INFO);
	}

	@Override
	protected void onPause() {
		super.onPause();
		mBeginTime = mVideoView.getCurrentPosition();
		L.v(this.getClass(), "onPause mBeginTime = " + mBeginTime);
		mSp.putIntValue(mVideoInfo.getId(), mBeginTime);
	}

	@Override
	protected void onStop() {
		super.onStop();
		if (0 < mBeginTime) {
			new SubmitTimeTask(this).execute(mBeginTime);
		}
	}

	private class SubmitTimeTask extends SimpleAsyncTask<Integer, Void, Boolean> {

		public SubmitTimeTask(Context context) {
			super(context);
		}

		@Override
		protected Boolean onLoad(Integer... params) throws Exception {
			return mVideoInfo.submitTime(mUser, params[0]);
		}

		@Override
		protected void onUI(Boolean result) {
		}

	}

	@Override
	public boolean onKeyUp(int keyCode, KeyEvent event) {
		if (KeyEvent.KEYCODE_MENU == keyCode) {
			toggleMenu();
		}
		return super.onKeyUp(keyCode, event);
	}

	@Override
	public void onClick(View v) {
		if (v.getId() == mVideoView.getId()) toggleMenu();
	}

	private void toggleMenu() {
		mIsMenuShown = !mIsMenuShown;
		if (mIsMenuShown) {
			mMediaController.show();
		} else {
			mMediaController.hide();
		}
	}

	@Override
	public void onPrepared(MediaPlayer mp) {
		mVideoView.post(new Runnable() {
			@Override
			public void run() {
				mVideoView.seekTo(mBeginTime);
				mVideoView.start();
				if (mVideoView.isPlaying()) mLoading.v();
			}
		});
	}

}
